<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="秒杀订单价格" prop="orderPrice">
      <el-input v-model="dataForm.orderPrice" placeholder="秒杀订单价格"></el-input>
    </el-form-item>
    <el-form-item label="秒杀订单状态" prop="orderStatic">
      <el-input v-model="dataForm.orderStatic" placeholder="秒杀订单状态"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="shopPrice">
      <el-input v-model="dataForm.shopPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="秒杀库存" prop="orderStock">
      <el-input v-model="dataForm.orderStock" placeholder="秒杀库存"></el-input>
    </el-form-item>
    <el-form-item label="商品名称" prop="shopName">
      <el-input v-model="dataForm.shopName" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="prc">
      <el-input v-model="dataForm.prc" placeholder="商品图片"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderPrice: '',
          orderStatic: '',
          shopPrice: '',
          orderStock: '',
          shopName: '',
          prc: ''
        },
        dataRule: {
          orderPrice: [
            { required: true, message: '秒杀订单价格不能为空', trigger: 'blur' }
          ],
          orderStatic: [
            { required: true, message: '秒杀订单状态不能为空', trigger: 'blur' }
          ],
          shopPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          orderStock: [
            { required: true, message: '秒杀库存不能为空', trigger: 'blur' }
          ],
          shopName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          prc: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/shop/order/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderPrice = data.order.orderPrice
                this.dataForm.orderStatic = data.order.orderStatic
                this.dataForm.shopPrice = data.order.shopPrice
                this.dataForm.orderStock = data.order.orderStock
                this.dataForm.shopName = data.order.shopName
                this.dataForm.prc = data.order.prc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/order/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderPrice': this.dataForm.orderPrice,
                'orderStatic': this.dataForm.orderStatic,
                'shopPrice': this.dataForm.shopPrice,
                'orderStock': this.dataForm.orderStock,
                'shopName': this.dataForm.shopName,
                'prc': this.dataForm.prc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
